﻿(function () {
    // var isHidden=!!$('#_fh21_swt_iask_question').length;
    // if(!isHidden){
    //     return;
    // }
    var right_defaultVal = "输入您的问题，10分钟内即可得到多名专家的解答";

    //一、拼接dom

    var menuOnWrapper = $("<div class='btm_right_float tw_rb_on menuOn' style='display:block;right:0px;bottom:0px'><ul></ul><form id='answerForm' action='" + FHUrls.iask + "/question/questionpresent' method='post' target='_blank'><a class='tw_rb_close closeForm'></a><textarea id='content' class='placeholder' name='word' placeholder='" + right_defaultVal + "' ></textarea><input type='submit' value=''/></form><a href='"+FHUrls.help+"/download/app?type=patient' target='_blank' class='tw_rb_myyz'>下载飞华APP  随时随地问医生 </a></div>");

    var menuOffWrapper = $("<div class='btm_right_float tw_rb_off menuOff' style='display:none;right:0px;bottom:100px'><ul></ul><a class='tw_rb_close exist'></a><p class='quickAsk'></p></div>");

    //二、注册事件
    function bindRightEvent() {


        var menuOn = $(".menuOn");
        var menuOff = $(".menuOff");

        var quickAsk = $(".quickAsk");
        var exist = $(".exist");
        var closeForm = $(".closeForm");

        quickAsk.bind("click", function () {
            menuOff.css("display", "none");
            menuOn.css("display", "block");
			//写入cookie
			$.cookie("formstate","open",{expires:7});
        });


        exist.bind("click", function (e) {
            e.stopPropagation();
			
            menuOff.css("display", "none");
            menuOn.css("display", "block");
			//写入cookie
			$.cookie("formstate","open",{expires:7});
        });

        closeForm.bind("click", function (e) {
            e.stopPropagation();
            menuOff.css("display", "block");
            //$("#content").val("");
            menuOn.css("display", "none");
			//写入cookie
			$.cookie("formstate","close",{expires:7});
        })

        $("#content").bind({
            "focusin": function (e) {
				e.stopPropagation();
				
				$(this).removeClass("blur").addClass("focus");
                if ($(this).val() == right_defaultVal) {
                    $(this).val("");
                }
            },
            "focusout": function (e) {
				e.stopPropagation();
				 
				$(this).removeClass("focus").addClass("blur")
                if ($(this).val().length == 0) {
                    $(this).val(right_defaultVal);
                }
            }
        });

        $("#answerForm").bind("submit", function () {

            //获取提交表单数据
            var contentControl = $("#content");
            var content = contentControl.val();

            if (content == right_defaultVal) {
               contentControl.val("");
            }
            $("#submit").submit();
        })
    };


    //判断是否支持placeholder
    function isPlaceholder() {
        var input = document.createElement('input');
        return 'placeholder' in input;
    }

    //三、添加到页面
    $(function () {
        var isFrom360onebox=$.cookie("from")=="360onebox";
        if(!isFrom360onebox){

        $("body").append(menuOnWrapper).append(menuOffWrapper);
        bindRightEvent();
		//获取cookie值
		var formstate=$.cookie("formstate");
		
		if(formstate!="undefined"&&formstate=="close"){
			$(".menuOn").css("display","none");
			$(".menuOff").css("display","block");
		}else{
			$(".menuOn").css("display","block");
			$(".menuOff").css("display","none");
		}
		
        if (!isPlaceholder()) {
            $("input.placeholder,textarea.placeholder").each(function () {
                $(this).val($(this).prop("placeholder"));
                $(this).focusin(function () {
                    if ($(this).val() == $(this).prop("placeholder")) {
                        $(this).val("");
                    }
                }).focusout(function () {
                    if ($(this).val() == "") {
                        $(this).val($(this).prop("placeholder"));
                    }
                });
            });
        }

        }
    });
})();